// pages/car/cz/cz.js
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
list: []
	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		wx.getStorage({
			key:'uid',
			success:(e)=>{
				console.log(e)
				this.setData({
					name: options.name,
					id:options.id,
					uid:e.data
				})
				wx.request({
					url: getApp().globalData.http + 'action=getByList',
					success: (e) => {
						console.log(e)
						this.setData({
							data: e.data.data
						})
					}
				})
			}
		})
	},
	bindTimeChange: function(e) {
			console.log(e)
			this.setData({
				time: e.detail.value
			})
		},
		journey(e) {
			this.setData({
				journey: e.detail.value
			})
		},
		gb(e) {
			let a = this.data.data
			let b=this.data.list
			a.forEach(function(item, index) {
					if (e.currentTarget.dataset.id == index) {
						if (a[index].state == undefined || a[index].state == 0) {
							a[index].state = 1
							b.push(a[index].names)
						} else {
							a[index].state = 0
							b.forEach(function(item,c){
								if(b[c]==a[index].names){
									b.splice(c,1)
								}
							})
						}
					}
				})
				console.log(b)
				this.setData({
					data: a,
					list:b
				})
			},
			over(){
				if(this.data.time!=undefined&&this.data.journey!=undefined&&this.data.list.length!=0){
					let a=this.data.list
					a.join(',')
					wx.request({
						url:getApp().globalData.http +'action=addBaoYang&carid='+this.data.id+'&bytime='+
						this.data.time+'&millions='+this.data.journey+'&byxm='+a+'&uid='+this.data.uid.uid,
						success:(e)=>{
							console.log(e)
							if(e.data.code==1){
								wx.navigateBack({
									delta:1
								})
							}else{
								wx.showToast({
									title:e.data.msg,
									icon:'none'
								})
							}
						}
					})
				}else{
					wx.showToast({
						icon:'none',
						title:'有未选项'
					})
				}
				
			}
	})